package  networkSimulation.framework

/**
 * This class represents a Node of a graph.
 *
 * @author  Tim Caljé
 *          Jeroen De Raedt
 *          Jorick Van Aken
 */
class Node(val name : String) extends Observable with FieldExporter {

  if(!this.isValid()) {
    throw new IllegalArgumentException("Invalid node")
  }

  def simulate[N<%Node, E<%Edge[N]](graph: Graph[N,E]) {
    // default: nothing happens
  }

  /**
   * Get the color of the node
   */
  def getColor() : String = "black"

  /**
   * Get the XML representation of this node
   */
  // We wanted this to be generic: so custom implementations of Node don't need to make changes to this method
  // So this hack collects all the fields of the class, looks for their get-methods and builds the XML representation
  // However by doing things this way, we demand that the attributes of the Node derivatives are public
  def toXml(): xml.Elem = {
     <node>
       <field name="name" tpe="string">{name}</field>{
       exportFields()
       }</node>
  }

  /**
   * String representation for this node
   */
  @Override
  override def toString() = name

  /**
   * Check whether this node is valid
   */
  protected def isValid() : Boolean = {
    if (this.name == "") {
      false
    } else {
      true
    }
  }

  /**
   * Custom equals method implementation
   * @return  Boolean
   *          True: this and the given object are equal
   *          False: this and the given object are not equal
   */
  @Override
  override def equals(that: Any) = that match {
   case other: Node => other.name == name 
   case _ => false
 }

}